package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Upload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        Upload upload = new Upload();
        String result = upload.imgUpload(req);
//        2@@放大手动阀@@532435@@235421@@三安光电发顺丰@@是@@是@@/loadImg/9_8hh4.jpg@@
        String[] results = result.split("@@");
        ProductModel productModel = new ProductModel();
        productModel.setCuisineId(Integer.parseInt(results[0]));
        productModel.setProductName(results[1]);
        productModel.setNormalPrice(Float.parseFloat(results[2]));
        productModel.setPrice(Float.parseFloat(results[3]));
        productModel.setContent(results[4]);
        productModel.setIsShow(Integer.parseInt(results[5]));
        productModel.setIsRecommend(Integer.parseInt(results[6]));
        productModel.setImgHref(results[7]);

        IProductService iProductService = new ProductServiceImpl();
        int count = iProductService.add(productModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
